package com.example.Controller;

import com.alibaba.fastjson.JSON;
import com.example.Req.LotteryTicketReq;
import com.example.common.BizException;
import com.example.common.Pagination;
import com.example.common.ResponseModel;
import com.example.model.FcapNoContent;
import com.example.model.LotteryTicketRes;
import com.example.model.TicketContent;
import com.example.model.LotteryTicket;
import com.example.service.LotteryTicketService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.hutool.core.util.NumberUtil.add;

@RestController
@Slf4j
@RequestMapping("api/qibuzhonga")
public class QiBuZhongController {


    @Autowired
    private LotteryTicketService lotteryTicketService;

    @RequestMapping("/list.do")
    public ResponseModel<String> getList(@RequestParam("type") Integer type){
        if(type == null || (type !=1 && type !=2)){
            throw new BizException("00000001", "参数错误");
        }
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);    //获取年
        int startYear = 1976;
        if(type ==2){
            startYear = 2020;
        }
        for(int i = startYear; i <= year; i++){
            lotteryTicketService.batchInsertLotteryTicket(i, type);
        }

        return new ResponseModel<>();
    }

    @RequestMapping("/calculate.do")
    public ResponseModel<String> calculate(@RequestParam("sort") String sort, @RequestParam("type") Integer type) throws Exception{
        if(type == null || StringUtils.isBlank(sort) || (type!=1 && type!=2)){
            throw new BizException("00000001", "参数错误");
        }
        lotteryTicketService.calculateRepeat(sort, type);
        return new ResponseModel<>();
    }

    /*@RequestMapping("/getRepeatListPage.do")
    public ResponseModel<Pagination<LotteryTicketRes>> getRepeatListPage(@RequestBody LotteryTicketReq req){
        ResponseModel<Pagination<LotteryTicketRes>> result = new ResponseModel<>();
        log.info("getRepeatListPage param:{}", JSON.toJSONString(req));
        if (req.getPageNo() == null) {
            req.setPageNo(1);
        }
        if (req.getPageSize() == null) {
            req.setPageSize(50);
        }
        if(req.getRepeat() == null){
            req.setRepeat(4l);
        }
        if(req.getNumber() == null){
            req.setNumber(1);
        }


        Pagination<LotteryTicketRes> pagination = lotteryTicketService.getRepeatList(req);
        log.info("getRepeatList size:{}", pagination.getList().size());
        result.setData(pagination);
        return result;
    }*/


    @RequestMapping("/getTicketListPage.do")
    public ResponseModel<Pagination<LotteryTicketRes>> getTicketListPage(@RequestBody LotteryTicketReq req){
        if(req == null || req.getType() == null || (req.getType()!=1 && req.getType()!=2 && req.getType()!=3 && req.getType()!=4)){
            throw new BizException("00000001", "参数错误");
        }
        ResponseModel<Pagination<LotteryTicketRes>> result = new ResponseModel<>();
        log.info("getRepeatListPage param:{}", JSON.toJSONString(req));
        if (req.getPageNo() == null) {
            req.setPageNo(1);
        }
        if (req.getPageSize() == null) {
            req.setPageSize(50);
        }

        if(StringUtils.isBlank(req.getNo())){
            req.setNo(null);
        }else{
            String content = req.getNo().replaceAll(":", ",")
                    .replaceAll("：", ",")
                    .replaceAll("#", ",");
            req.setNo(content);
        }


        Pagination<LotteryTicketRes> pagination = lotteryTicketService.getTicketListPage(req);
        log.info("getRepeatList size:{}", pagination.getList().size());
        result.setData(pagination);
        return result;
    }


   /* @RequestMapping("/syncNewData.do")
    public ResponseModel<String> syncNewData() throws Exception{
        lotteryTicketService.batchInsertLotteryTicket(1);
        lotteryTicketService.calculateNewDataRepeat("desc", 7);
        return new ResponseModel<>();
    }*/

    @RequestMapping("/insertOrUpdateData.do")
    public ResponseModel<String> insertOrUpdateData(@RequestBody LotteryTicketReq req) throws Exception{
        log.info("insertNewData:{}", JSON.toJSONString(req));
        if(req ==  null || StringUtils.isBlank(req.getContent())
                || StringUtils.isBlank(req.getNo())  || req.getType() == null || (req.getType()!=1 && req.getType()!=2 && req.getType()!=3 && req.getType()!=4)
                || req.getSerialNo() == null){
            log.error("insertNewData param is null");
            throw new BizException("00000001", "参数错误");
        }
        String content = req.getContent().replaceAll(":", ",")
                .replaceAll("：", ",")
                .replaceAll("，", ",");
        req.setContent(content);
        if(req.getId() != null){
            lotteryTicketService.updateLotteryTicket(req);
        }else{
            lotteryTicketService.insertLotteryTicket(req);
        }
        return new ResponseModel<>();
    }
    @RequestMapping("/deleteData.do")
    public ResponseModel<String> deleteData(@RequestBody LotteryTicketReq req) throws Exception{
        log.info("insertNewData:{}", JSON.toJSONString(req));
        if(req ==  null || req.getId() == null  || req.getType() == null || (req.getType()!=1 && req.getType()!=2 && req.getType()!=3 && req.getType()!=4)){
            log.error("insertNewData param is null");
            throw new BizException("00000001", "参数错误");
        }

        lotteryTicketService.delLotteryTicket(req);
        return new ResponseModel<>();
    }

/*    @RequestMapping("/jiSuanRepeat.do")
    public ResponseModel<String> jiSuanRepeat(@RequestBody Map<String, String> map) throws Exception{
        lotteryTicketService.jiSuanRepeat(map.get("calculate"),Integer.parseInt(map.get("type")));
        return new ResponseModel<>();
    }*/

    @GetMapping("/jsRepeat.do")
    public ResponseModel<String> jsRepeat(@RequestParam("type")int type, @RequestParam("small")Integer small, @RequestParam("big")Integer big) throws Exception{
        if(small == null || big == null || small>big ){
            throw new BizException("0000001", "参数错误");
        }
        lotteryTicketService.jsRepeat(type, small, big);

        return new ResponseModel<>();
    }

    @RequestMapping("/getBuyTicketList.do")
    public ResponseModel<List<LotteryTicketRes>> getBuyTicketList(@RequestBody LotteryTicketReq req){
        ResponseModel<List<LotteryTicketRes>> responseModel = new ResponseModel<>();
        if(req == null || req.getType() == null || (req.getType()!=1 && req.getType()!=2 && req.getType()!=3 && req.getType()!=4)){
            throw new BizException("00000001", "参数错误");
        }
        if(req == null){
            req = new  LotteryTicketReq();
            req.setRepeat(4l);
        }
        if(req.getRepeat() == null && req.getType()<3){
            req.setRepeat(4l);
        }
        if(req.getRepeat() == null && req.getType()>=3){
            req.setRepeat(4l);
        }
        List<LotteryTicketRes> list = lotteryTicketService.getBuyTicketList(req);
        responseModel.setData(list);
        return responseModel;
    }
}
